#-------------------------------------------
DEBUG_EN=1
APP=my_matlab
TOP_DIR=$(shell pwd)
ARCH=
CC=g++
CFLAGS= -Wall -Wfatal-errors -fpermissive 
#DLIBS= -lopencv_core -lopencv_imgproc -lopencv_highgui
DLIBS= -lm  -lopenblas -leng -lmx 
#CFLAGS= -Wall -Wfatal-errors -fpermissive `pkg-config --cflags --libs opencv`
#CFLAGS= -Wall  -fpermissive
LDFLAGS=
SRC_TOP=./src
OBJ_DIR=./obj/
INC_MATLAB=/usr/MATLAB/R2017a/extern/include
INC_DIRS=./inc
DEMO_DIRS=./demo/
LIB_DIRS=/usr/MATLAB/R2017a/bin/glnxa64


ifeq ($(DEBUG_EN), 1) 
CFLAGS+=-O0 -g -DDEBUG_EN -std=c++11
endif
# Dependencies if any
LIBS= m openblas
DEPS=$(wildcard ./inc/*.h)

CFLAGS+= $(foreach D,$(INC_DIRS),-I$D)
CFLAGS+= $(foreach D,$(INC_MATLAB),-I$D)
LDFLAGS+= $(foreach D,$(LIB_DIRS),-L$D) $(foreach L,$(LIBS), -l$(L))


#-------------------------------------------
# Source files for compilation
#-------------------------------------------

SRCS=$(wildcard $(SRC_TOP)/*.cpp)
#SRCS=$(wildcard $(SRC_TOP)/cpp_input_reader.cpp)

# List of all objects that are to be built
OBJS= $(addprefix $(OBJ_DIR), $(patsubst %.cpp, %.o, $(notdir $(SRCS))))

vpath %.cpp $(dir $(SRCS))

#-------------------------------------------
# Build targets
all : obj $(APP) $(DEPS)

my_so : obj $(OBJ_DIR) $(OBJS) $(DEPS)


$(APP): Makefile $(OBJ_DIR) $(OBJS)
	@echo "Linking..."
	$(CC)  -o  $(DEMO_DIRS)$@  $(OBJS) $(LDFLAGS) $(DLIBS)
	$(CC)   -pipe -fPIC -shared   -o  $(DEMO_DIRS)$@.so  $(OBJS) $(LDFLAGS) $(DLIBS)

$(OBJ_DIR):
	mkdir -p $(OBJ_DIR)
	
$(OBJS):$(OBJ_DIR)%.o:%.cpp
	@echo "Building $@ from $<..."
	$(CC)  -pipe -fPIC -shared   -c $(CFLAGS) $< -o $@ $(DLIBS)
	
#-------------------------------------------

#-------------------------------------------
# Phony targets
#-------------------------------------------
.phony: clean clean_all

obj :
	mkdir -p obj

# Clean the common object files
clean:
	@echo "Removing all objects..."
	rm -rf $(OBJS)
	rm -f $(APP)
	
# Clean all libraries if at all
ca: clean all 

# Print important variables and values
print:
	@echo "Compiler flags = $(CFLAGS)"
	@echo "Linker flags = $(LDFLAGS)"
	@echo "Source files = $(SRCS)"
	@echo "Object files = $(OBJS)"
	@echo "Dependencies = $(DEPS)"


my_run:
	g++  -Wall -Wfatal-errors -fpermissive  -O0 -g -DDEBUG_EN -std=c++11   $(SRCS)   $(foreach D,$(INC_DIRS),-I$D) $(foreach L,$(LIBS), -l$(L))    -o    my_test 

all_com:
	rm -rf ./obj/*.o
	g++  -pipe -fPIC -shared   -c -Wall -Wfatal-errors -fpermissive  -O0 -g -DDEBUG_EN -std=c++11 -I./inc -I/usr/MATLAB/R2017a/extern/include  -L/usr/MATLAB/R2017a/bin/glnxa64      -leng -lmx   ./src/fft_checker.cpp -o obj/fft_checker.o
	g++  -pipe -fPIC -shared   -c -Wall -Wfatal-errors -fpermissive  -O0 -g -DDEBUG_EN -std=c++11 -I./inc -I/usr/MATLAB/R2017a/extern/include  -L/usr/MATLAB/R2017a/bin/glnxa64      -leng -lmx   ./src/gen_wave.cpp -o obj/gen_wave.o
	g++   -pipe -fPIC -shared -o ./demo/my_matlab.so  obj/*.o   -lm  -lopenblas -L/usr/MATLAB/R2017a/bin/glnxa64      -leng -lmx 
	g++  -o  ./demo/my_matlab   ./obj/*.o    -lm  -lopenblas  	 -L/usr/MATLAB/R2017a/bin/glnxa64      -leng -lmx


